package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import resources.ResourceProvider;

/**
 *  Create SearchPanel class
 */
public class SearchPanel extends JPanel
{
    private static final long serialVersionUID = -3279816271922871553L;
    
    private MailDisplayArea mailDisplayArea;
    
    private JLabel textLabel;
    
    private JLabel subject;
    
    private JTextField subjectTextfield;
    
    private JLabel from;
    
    private JTextField fromTextfield;
    
    private JLabel hasWords;
    
    private JButton advancedSearchButton;
    
    private JTextField hasWordsField;
    
    private JLabel AdvancedSearchLabel;
    
    private JTextField textField;
    
    private JLabel regExSearchLabel;
    
    private JTextField regExSearchField;
    
    private JButton searchButton;
    
    private JButton searchRegExButton;

    /**
     *  Constructor SearchPanel
     *  @param mailDisplayArea the mail display area reference
     */
    public SearchPanel(MailDisplayArea mailDisplayArea)
    {
        this.mailDisplayArea = mailDisplayArea;
        this.initialize();
    }
    
    /**
     *  Create one commonTextFieldRectangle
     *  @param an integer value: location of y 
     *  @return return the Rectangle component at given location
     */
    private Rectangle commonTextFieldRectangle(int setY)
    {
    	return new Rectangle(135,setY,400,30);
    }
    
    /**
     *  Create one commonJLabelRectangle
     *  @param an integer value: location of y
     *  @return return the Rectangle component at given location
     */
    private Rectangle commonJLabelRectangle(int setY)
    {
    	return new Rectangle(32,setY,64,30);
    }
    
    /**
     *  Create one commonJButtonRectangle
     *  @param an integer value: location of y 
     *  @return return the Rectangle component at given location
     */
    @SuppressWarnings("unused")
	private Rectangle commonJButtonRectangle(int setY)
    {
    	return new Rectangle(555, setY, 110, 27);
    }
    
    /**
     * Initialization of the Search Panel 
     */
    private void initialize()
    {
        TitledBorder title = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Search Options");
        this.setBorder(title);
        this.setLayout(null);
                
        textLabel = new JLabel("Search");
        textLabel.setBounds(new Rectangle(32,44,64,30));
        textLabel.setFont(new Font("Times New Roman",Font.PLAIN,16));
        this.add(textLabel);
        
        textField = new JTextField();
        textField.setBounds(new Rectangle(135,44,400,30));
        this.add(textField);
        
        regExSearchLabel = new JLabel("RegEx Search");
        regExSearchLabel.setBounds(new Rectangle(32,84,64,30));
        regExSearchLabel.setFont(new Font("Times New Roman",Font.PLAIN,16));
        this.add(regExSearchLabel);
        
        regExSearchField = new JTextField();
        regExSearchField.setBounds(new Rectangle(135,84,400,30));
        this.add(regExSearchField);       
        
        searchButton = new JButton(ResourceProvider.SEARCH);
        searchButton.setBounds(new Rectangle(555,44,110,27));
        
        searchButton.addActionListener(new ActionListener() {
            
            public void actionPerformed(ActionEvent e)
            {
                SearchPanel.this.mailDisplayArea.displaySearchResuls(textField.getText(), false);
            }
        });
        this.add(searchButton);      
        
        searchRegExButton = new JButton(ResourceProvider.SEARCH_REGEX);
        searchRegExButton.setBounds(new Rectangle(555, 84, 110, 27));
        searchRegExButton.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e)
        	{
        		SearchPanel.this.mailDisplayArea.displaySearchResuls(regExSearchField.getText(), true);
        	}
        });
        this.add(searchRegExButton);
        
        //***************Advanced Search Panel**************************
        
        AdvancedSearchLabel = new JLabel("Search emails with");
        AdvancedSearchLabel.setBounds(new Rectangle(32,130,200,30));
        AdvancedSearchLabel.setFont(new Font("Times New Roman",Font.PLAIN,20));
        this.add(AdvancedSearchLabel);
        
        subject = new JLabel("Subject");
        subject.setBounds(this.commonJLabelRectangle(170));
        subject.setFont(new Font("Times New Roman",Font.PLAIN,16));
        this.add(subject);
        
        subjectTextfield = new JTextField();
        subjectTextfield.setBounds(commonTextFieldRectangle(170));
        this.add(subjectTextfield);
        
        from = new JLabel("From");
        from.setBounds(this.commonJLabelRectangle(210));
        from.setFont(new Font("Times New Roman",Font.PLAIN,16));
        this.add(from);
        
        fromTextfield = new JTextField();
        fromTextfield.setBounds(commonTextFieldRectangle(210));
        this.add(fromTextfield);
           
        hasWords = new JLabel("Has the words");
        hasWords.setBounds(this.commonJLabelRectangle(250));
        hasWords.setFont(new Font("Times New Roman",Font.PLAIN,16));
        this.add(hasWords);
        
        hasWordsField = new JTextField();
        hasWordsField.setBounds(commonTextFieldRectangle(250));
        this.add(hasWordsField);
        
        advancedSearchButton = new JButton(ResourceProvider.SEARCH);
        advancedSearchButton.setBounds(new Rectangle(32,290,110, 27));
        
        advancedSearchButton.addActionListener(new ActionListener(){
        	public void actionPerformed(ActionEvent e)
        	{
        		SearchPanel.this.mailDisplayArea.displayAdvancedSearchResuls(
        				fromTextfield.getText(), subjectTextfield.getText(), hasWordsField.getText());
        	}
        });
        this.add(advancedSearchButton);            
    }
}
